home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / MakeWrite / MakeWrite Folder / MWGen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  8.7 KB  |  327 lines  |  [TEXT/KAHL]

  1. # include    "MWMaca.h"
  2. # include    "MWFileStuff.h"
  3. # include    "MakeWrite.h"
  4.  
  5. # ifdef debug
  6. # include    "TransDisplay.h"
  7. # endif
  8.  
  9.  
  10. /*
  11.  * Prototype for data of empty paragraph
  12.  */
  13.  
  14. typedef struct EmptyParaData6
  15. {
  16.     short    pTextLen;
  17.     short    pFmtLen;
  18.     Format    pFormat;
  19. } EmptyParaData6;
  20.  
  21.  
  22. EmptyParaData6    emptyParaData =
  23. {
  24.     0,                                /* text length = 0 (no text) */
  25.     6,                                /* format data length (1 format) */
  26.     { 0, 12, 0, 1}                    /* format: pos 0, size 12, style */
  27.                                     /* plain, application font */
  28. };
  29.  
  30.  
  31. UPrintRec    uPrintRec;    /* not initialized - doesn't matter? */
  32.  
  33.  
  34. Ruler    stdRuler =
  35. {
  36.     10,                                /* left margin */
  37.     480,                            /* right margin */
  38.     leftJust,                        /* justification */
  39.     10,                                /* number of tabs */
  40.     0,                                /* line spacing = single */
  41.     10,                                /* indentation of first line */
  42.     { 40, 80, 120, 160, 200,        /* tab values */
  43.       240, 280, 320, 360, 400 },    /* tab values */
  44.     { 0, 0, 0, 0 }                    /* four bytes fill */
  45. };
  46.  
  47.  
  48.  
  49. Globals6    globals =
  50. {
  51.     6,                                /* MacWrite 4.5 = version 6 */
  52.     0,                                /* main doc para count - fill in later */
  53.     2,                                /* header para count */
  54.                                     /* (one ruler + 1 empty text para) */
  55.     2,                                /* footer para count */
  56.                                     /* (one ruler + 1 empty text para) */
  57.     0,                                /* title page flag */
  58.     0,                                /* unused */
  59.     0,                                /* scrap not displayed */
  60.     0,                                /* footer not displayed */
  61.     0,                                /* header not displayed */
  62.     0,                                /* rulers displayed */
  63.     -1,                                /* active document: -1 tells MacWrite */
  64.                                     /* to recalc display information */
  65.     1,                                /* starting page = 1 */
  66.     0L,                                /* free list position - fill in later */
  67.     8,                                /* free list length */
  68.     8,                                /* # bytes allocated for list */
  69.     { 0, 0, 0, 0, 0, 0, 0,            /* unused */
  70.       0, 0, 0, 0, 0, 0, 0 }            /* unused */
  71. };
  72.  
  73.  
  74. Windows6    windowVars =
  75. {
  76.     { 1, 0 },                        /* start of selection (para 1, char 0) */
  77.     { 1, 0 },                        /* end of selection (para 1, char 0) */
  78.     0,                                /* vertical offset */
  79.     0,                                /* first paragraph to redraw */
  80.     0L,                                /* position of information array */
  81.                                     /* (filled in later) */
  82.     0,                                /* length of information array */
  83.                                     /* (filled in later) */
  84.     0L,                                /* position of line height array */
  85.                                     /* (filled in later) */
  86.     0,                                /* length of line height array */
  87.                                     /* (filled in later) */
  88.     { -14, 30 },                    /* page icon position */
  89.     { -14, 236 },                    /* date icon position */
  90.     { -14, 442 },                    /* time icon position */
  91.     { -1, -1, -1, -1 },                /* four bytes fill */
  92.     -1,                                /* redraw ovals (icons) */
  93.     0,                                /* don't redraw rulers */
  94.     { -1, -1 },                        /* face active when saved */
  95.                                     /* (-1 = "has never been set") */
  96.     -1                                /* font active when saved */
  97.                                     /* (-1 = "has never been set") */
  98. };
  99.  
  100.  
  101. ParaInfo6    rulerParaInfo;
  102. ParaInfo6    nullTextParaInfo;
  103.  
  104.  
  105. Boolean
  106. SetParaInfo (ParaInfo6 *p, short height, Byte status,
  107.                 long dataPos, short dataLen, short comFormats)
  108. {
  109.  
  110. # ifdef    debug
  111.     DisplayString ("\p{SetParaInfo}");
  112. # endif
  113.  
  114.     p->paraHeight = height;
  115.     p->paraPos = 0;
  116.     p->paraHandle = (long) nil;                /* paraHandle is never actually used */
  117.     p->paraStOff.paraOffset = dataPos;        /* set this first */
  118.     p->paraStOff.paraStatus = status;        /* *then* this! */
  119.     p->paraLen = dataLen;
  120.     p->paraFmts = comFormats;
  121.  
  122.     return (true);
  123. }
  124.  
  125.  
  126. /*
  127.  * Write file prelude, using prototype structures.
  128.  *    Global variables
  129.  *    Universal print record
  130.  *    Footer document variables
  131.  *    Header document variables
  132.  *    Main document varables
  133.  *
  134.  * Some of these will be patched in the postlude.
  135.  *
  136.  * Also write as much of the invariant file contents as possible:
  137.  *    Footer document contents (1 ruler + 1 empty text paragraph)
  138.  *    Header document contents (1 ruler + 1 empty text paragraph)
  139.  *    Initial ruler of main document
  140.  */
  141.  
  142. Boolean
  143. WritePrelude (short f)
  144. {
  145.  
  146.     return (FileWrite (f, (Ptr) &globals, (long) sizeof (Globals6))
  147.         && FileWrite (f, (Ptr) &uPrintRec, (long) sizeof (UPrintRec))
  148.         && FileWrite (f, (Ptr) &windowVars, (long) sizeof (Windows6))
  149.         && FileWrite (f, (Ptr) &windowVars, (long) sizeof (Windows6))
  150.         && FileWrite (f, (Ptr) &windowVars, (long) sizeof (Windows6))
  151.         && ZeroPad (f, 6)
  152.         && FileWrite (f, (Ptr) &stdRuler, (long) sizeof (Ruler))
  153.         && FileWrite (f, (Ptr) &emptyParaData, (long) sizeof (EmptyParaData6))
  154.         && FileWrite (f, (Ptr) &stdRuler, (long) sizeof (Ruler))
  155.         && FileWrite (f, (Ptr) &emptyParaData, (long) sizeof (EmptyParaData6))
  156.         && FileWrite (f, (Ptr) &stdRuler, (long) sizeof (Ruler))
  157.     );
  158. }
  159.  
  160.  
  161. /*
  162.  * Write line height array, one entry for each paragraph.  First
  163.  * para is always a ruler - write a zero word.  Other paras are
  164.  * faked by writing information that there's one line in the para
  165.  * of height 16 (12-point height).  MacWrite will fix when it reads
  166.  * the file.
  167.  *
  168.  * paraCount assumed >= 2.
  169.  */
  170.  
  171. Boolean
  172. WriteLineHeights (short f, short paraCount)
  173. {
  174.  
  175.     if (!WriteInteger (f, 0))
  176.         return (false);
  177.  
  178.     while (--paraCount > 0)
  179.     {
  180.         if (!WriteLongint (f, 0x00011010))
  181.             return (false);
  182.     }
  183.     
  184.     return (true);
  185. }
  186.  
  187.  
  188. /*
  189.  * Write line height and paragraph info positions and lengths
  190.  */
  191.  
  192. Boolean
  193. WriteArrayInfo (short f, long seek, long piPos, short piLen,
  194.                                             long lhPos, short lhLen)
  195. {
  196.  
  197.     FileSeek (f, seek);
  198.     return (WriteLongint (f, piPos) && WriteInteger (f, piLen)
  199.             && WriteLongint (f, lhPos) && WriteInteger (f, lhLen));
  200. }
  201.  
  202.  
  203. /*
  204.  * Paragraph data are all written now.  The line height and paragraph
  205.  * info arrays must now be written, as well as an empty free list.
  206.  * Save the positions of these arrays, as they must be patched into
  207.  * the window variables.
  208.  *
  209.  * The paraCount and paraInfo are for the text paragraphs only.
  210.  * When the main document paragraph count and paragraph information
  211.  * are length are patched into the globals, increment to take
  212.  * into account the initial ruler (written during prelude).
  213.  */
  214.  
  215. Boolean
  216. WritePostlude (short f, short paraCount, PIAHandle paraInfo)
  217. {
  218. long    fLHPos, fPIPos;        /* footer line height, para info array pos */
  219. long    hLHPos, hPIPos;        /* header line height, para info array pos */
  220. long    mLHPos, mPIPos;        /* main line height, para info array pos */
  221. short    paraInfoLen;        /* length of main para info array */
  222. short    defInfoLen;            /* length of header, footer pi array */
  223. short    defLHLen;            /* length of header, footer line height array */
  224. short    mainLHLen;            /* length of main line height array */
  225. long    freeListPos;        /* free list pos */
  226. Boolean    result;
  227.  
  228.     /*
  229.      * Set up prototype ruler paragraph and null text paragraph
  230.      * information array elements.  The position field must be filled
  231.      * in on a per-window basis.
  232.      */
  233.  
  234.     SetParaInfo (&rulerParaInfo, 0, 0, (long) 0x130, 34, 0);
  235.     SetParaInfo (&nullTextParaInfo, 16, 0, (long) 0x152, 10, 0);
  236.  
  237.     /*
  238.      * For footer
  239.      */
  240.  
  241.     fLHPos = FilePos (f);
  242.     if (!WriteLineHeights (f, 2))
  243.         return (false);
  244.  
  245.     fPIPos = FilePos (f);
  246.     if (!FileWrite (f, (Ptr) &rulerParaInfo, (long) sizeof (ParaInfo6))
  247.         || !FileWrite (f, (Ptr) &nullTextParaInfo, (long) sizeof (ParaInfo6)))
  248.             return (false);
  249.  
  250.     /*
  251.      * For header
  252.      */
  253.  
  254.     hLHPos = FilePos (f);
  255.     if (!WriteLineHeights (f, 2))
  256.         return (false);
  257.  
  258.     rulerParaInfo.paraStOff.paraOffset += sizeof (Ruler) + sizeof (EmptyParaData6);
  259.     nullTextParaInfo.paraStOff.paraOffset += sizeof (Ruler) + sizeof (EmptyParaData6);
  260.  
  261.     hPIPos = FilePos (f);
  262.  
  263.     if (!FileWrite (f, (Ptr) &rulerParaInfo, (long) sizeof (ParaInfo6))
  264.         || !FileWrite (f, (Ptr) &nullTextParaInfo, (long) sizeof (ParaInfo6)))
  265.             return (false);
  266.  
  267.     /*
  268.      * For main
  269.      */
  270.  
  271.     mLHPos = FilePos (f);
  272.     if (!WriteLineHeights (f, paraCount + 1))
  273.         return (false);
  274.  
  275.     rulerParaInfo.paraStOff.paraOffset += sizeof (Ruler) + sizeof (EmptyParaData6);
  276.  
  277.     mPIPos = FilePos (f);
  278.  
  279.     if (!FileWrite (f, (Ptr) &rulerParaInfo, (long) sizeof (ParaInfo6)))
  280.         return (false);
  281.  
  282.     paraInfoLen = paraCount * sizeof (ParaInfo6);
  283.     HLock ((Handle) paraInfo);
  284.     result = FileWrite (f, (Ptr) *paraInfo, (long) paraInfoLen);
  285.     HUnlock ((Handle) paraInfo);
  286.     if (!result)
  287.         return (false);
  288.  
  289.     /*
  290.      * Write free list.  It's at the end of the file, so set the
  291.      * file EOF here, too.
  292.      */
  293.  
  294.     freeListPos = FilePos (f);
  295.     if (!WriteLongint (f, freeListPos + 8) || !WriteLongint (f, 0L))
  296.         return (false);
  297.  
  298.     (void) SetEOF (f, FilePos (f));
  299.  
  300.     /*
  301.      * Now patch globals and window variables:
  302.      *    main doc para count (increment to account for ruler)
  303.      *    free list position
  304.      *    line height and para info array positions (and para info array
  305.      *    length for main document)
  306.      */
  307.  
  308.     FileSeek (f, 2L);
  309.     ++paraCount;
  310.     paraInfoLen += sizeof (ParaInfo6);
  311.     if (!WriteInteger (f, paraCount))
  312.         return (false);
  313.     FileSeek (f, 18L);
  314.     if (!WriteLongint (f, freeListPos))
  315.         return (false);
  316.  
  317.     defInfoLen = 2 * sizeof (ParaInfo6);
  318.     defLHLen = 3 * sizeof (short);
  319.     mainLHLen = sizeof (short) + (paraCount-1) * sizeof (long);
  320.     if (!WriteArrayInfo (f, 172L, fPIPos, defInfoLen, fLHPos, defLHLen)
  321.         || !WriteArrayInfo (f, 218L, hPIPos, defInfoLen, hLHPos, defLHLen)
  322.         || !WriteArrayInfo (f, 264L, mPIPos, paraInfoLen, mLHPos, mainLHLen))
  323.             return (false);
  324.  
  325.     return (true);
  326. }
  327.